﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam.Quản_lý_sách.Quản_lý_nhà_xuất_bản
{
    public partial class FormEditNXB : Form
    {
        private CXulyNhaxuatban xuly;
        string maeditnxb;
        int id;
        string no;

        public FormEditNXB(int button,string manxb)
        {
            InitializeComponent();
            if (button == 0)
                btnchung.Text = "Add";
            else
                btnchung.Text = "Save";
            maeditnxb = manxb;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnchung_Click(object sender, EventArgs e)
        {
            if (btnchung.Text == "Add")
            {
                Kết_nối.NhaXuatBan nxb = new Kết_nối.NhaXuatBan();
                nxb.NhaXuatBanID = xuly.GetdsNhaXuatBan().Last().NhaXuatBanID + 1;
                nxb.NhaXuatBanNo = string.Concat("NXB", nxb.NhaXuatBanID.ToString());
                nxb.NhaXuatBanName = txtten.Text;
                nxb.NhaXuatBanAddress = txtdiachi.Text;
                nxb.NhaXuatBanPhoneNumber = txtsdt.Text;
                nxb.NhaXuatBanEmail = txtemail.Text;
                nxb.NhaXuatBanDescription = txtdes.Text;
                nxb.NhaXuatBanStatus = "live";
                xuly.ThemNhaXuatBan(nxb);
                Close();
            }
            else
            {
                Kết_nối.NhaXuatBan nxb = new Kết_nối.NhaXuatBan();
                nxb.NhaXuatBanID = id;
                nxb.NhaXuatBanNo = no;
                nxb.NhaXuatBanName = txtten.Text;
                nxb.NhaXuatBanAddress = txtdiachi.Text;
                nxb.NhaXuatBanPhoneNumber = txtsdt.Text;
                nxb.NhaXuatBanEmail = txtemail.Text;
                nxb.NhaXuatBanDescription = txtdes.Text;
                xuly.SuaNhaXuatBan(nxb);
                Close();
            }
        }

        private void FormEditNXB_Load(object sender, EventArgs e)
        {
            xuly = new CXulyNhaxuatban();
            if (btnchung.Text == "Save")
            {
                Kết_nối.NhaXuatBan editnxb = xuly.TimNhaXuatBanma(maeditnxb);
                if (editnxb != null && editnxb.NhaXuatBanStatus == "live")
                {
                    id = editnxb.NhaXuatBanID;
                    no = editnxb.NhaXuatBanNo;
                    txtten.Text = editnxb.NhaXuatBanName;
                    txtdiachi.Text = editnxb.NhaXuatBanAddress;
                    txtsdt.Text = editnxb.NhaXuatBanPhoneNumber;
                    txtemail.Text = editnxb.NhaXuatBanEmail;
                    txtdes.Text = editnxb.NhaXuatBanDescription;
                }
            }
        }
    }
}
